package cn.netuo.bumbersoll.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.text.TextUtils;

import com.rooten.base.DBBase;

import java.util.ArrayList;

import lib.grasp.util.L;

/**
 * 系统参数表
 */
public class TableArg extends DBBase {

    private final String    TB_NAME   	            = "tb_arg";

    private final String    COL_ARG_NAME           = "arg_name";           //参数名
    private final String    COL_ARG_VALUE          = "arg_value";          //参数值

    public TableArg() {
        createTable();
    }

    //建表
    private boolean createTable() {
        if (tableExists(TB_NAME) && !hasColumn(TB_NAME, COL_ARG_NAME)) {    // 存在表结构不对的同名表
            dropTable(TB_NAME);
            return createTable();
        }
        else if (tableExists(TB_NAME)) {    // 存在表
            return true;
        }
        try {
            String strFmt = "CREATE TABLE %s ("
                    + "%s TEXT PRIMARY KEY,"        //主键
                    + "%s TEXT "
                    + ");";

            String strSql = String.format(strFmt, 	TB_NAME,
                    COL_ARG_NAME,
                    COL_ARG_VALUE);
//            L.logOnly("建表--SQL:", strSql);
            execSQL(strSql);
            return true;
        }
        catch (SQLException e) {
            L.logOnly("SQLException:" + e.getMessage());
        }
        return false;
    }

    //删表
    public void dropTable() {
        if (tableExists(TB_NAME)) dropTable(TB_NAME);
    }

    //增(insert into 表 values * )
    public void importArg(String argName, String argValue) {
        if(TextUtils.isEmpty(argName) || TextUtils.isEmpty(argValue)) return;
        ContentValues cv = new ContentValues();
        cv.put(COL_ARG_NAME 		, argName);
        cv.put(COL_ARG_VALUE		, argValue);

//        insert(TB_NAME, cv);
        replace(TB_NAME, cv);
    }

    //查(select * from 表 where 日期<(比时间点start_date早的count个数据))
    public String queryByArgName(String argName) {
        if(TextUtils.isEmpty(argName)) return "";
        ArrayList<String> alarm_List = new ArrayList<>();
        String sql = "select %s from %s where %s = '%s';";
        sql = String.format(sql, COL_ARG_VALUE, TB_NAME, COL_ARG_NAME, argName);
//        L.logOnly("查--SQL:", sql);

        String value = "";

        Cursor result = rawQuery(sql);
        try {
            for (result.moveToFirst(); !result.isAfterLast(); result.moveToNext()){  // 使用游标获取结果{
                value   = result.getString(result.getColumnIndex(COL_ARG_VALUE));
            }
        }
        catch (Exception e){
            L.logOnly("Exception--" + e);
        }
        finally {
            closeAll(result);
        }
        return value;
    }

    //改(update 表 set 状态=  where MsgID= )
    public boolean updateArgValue(String argName, String argValue) {
        if(TextUtils.isEmpty(argName) || TextUtils.isEmpty(argValue)) return false;
        try{
            String sql = "update %s set %s = '%s' where %s = '%s';";
            sql = String.format(sql, TB_NAME, COL_ARG_VALUE, argValue, COL_ARG_NAME, argName);
//            L.logOnly("改--SQL:", sql);
            execSQL(sql);
            return true;
        }
        catch (Exception e){
            L.logOnly("Exception" + e.getMessage());
            return false;
        }
    }

    //删(delete from 表 where msgID = )
    public void deleteByArgName(String argName) {
        if(TextUtils.isEmpty(argName)) return;
        String sql = "delete from %s where %s = '%s';";
        sql = String.format(sql, TB_NAME, COL_ARG_NAME, argName);
//        L.logOnly("删--SQL:", sql);
        execSQL(sql);
    }
}
